#-*- codeing = utf-8 -*-
#@Time : 2020/9/8 0:14
#@Author : 阳某
#@File : pdf_pypdf2.py
#@Software : PyCharm

import PyPDF2
reader = PyPDF2.PdfFileReader(open('linux.pdf', 'rb'))
print(reader.getNumPages()) # 获取pdf总页数
print(reader.isEncrypted) # 判断是否有加密
page = reader.getPage(4) # 获取第四页
print(page.extractText()) # 获取第四页的内容
print(reader.getDocumentInfo()) # 获取PDF元信息，即创建时间，作者，标题等


import PyPDF2
reader = PyPDF2.PdfFileReader(open('linux.pdf', 'rb'))
output = PyPDF2.PdfFileWriter()
output.addPage(reader.getPage(1))
output.addPage(reader.getPage(4))
output.addPage(reader.getPage(5))
print(output.getNumPages()) # 获取写入页的总页数
output.encrypt('123456')
outputStream = open('PyPDF2-output.pdf', 'wb')
output.write(outputStream)
outputStream.close()



import PyPDF2
reader = PyPDF2.PdfFileReader(open('linux.pdf', 'rb'))
page = reader.getPage(0) # 获取第0页
page.rotateClockwise(180) # 旋转180度
writer = PyPDF2.PdfFileWriter() # 创建PDF写入的对象
writer.addPage(page)
outputStream = open('rotate-page-test.pdf', 'wb') # 创建一个PDF文件
writer.write(outputStream) # 往文件写入PDF数据
outputStream.close() # 写入流


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import PyPDF2

reader = PyPDF2.PdfFileReader(open('linux.pdf', 'rb')) # 增加水印的原文件

watermark = PyPDF2.PdfFileReader(open('水印模板.pdf', 'rb')) # 水印的模板

writer = PyPDF2.PdfFileWriter() # 写入PDF的对象

for i in range(reader.getNumPages()):
  page = reader.getPage(i)
  page.mergePage(watermark.getPage(0)) # 将原文件与水印模板合并
  writer.addPage(page) # 增加到写入对象中

outputStream = open('watermark-test-linux.pdf', 'wb') # 打开一个写入硬盘的文件对象
writer.write(outputStream) # 将合并好的数据，写入硬盘中
outputStream.close() # 关闭文件句柄


#!/usr/bin/env python
# -*- coding: utf-8 -*-

from PyPDF2 import PdfFileMerger

merger = PdfFileMerger() # 创建一个合并的对象

input1 = open('01PDF.pdf', 'rb')
input2 = open('02PDF.pdf', 'rb')
input3 = open('03PDF.pdf', 'rb')

merger.append(fileobj=input1, pages=(0, 3)) # 合并文件1的0到3页
merger.merge(position=2, fileobj=input2, pages=(0, 1)) # 合并文件2的0到1页
merger.append(fileobj=input3) # 合并文件的所有页

output = open('document-output.pdf', 'wb') # 保存硬盘上
merger.write(output) # 写入到硬盘上
output.close() # 关闭文件句柄



#!/usr/bin/env python
# -*- coding: utf-8 -*-

import PyPDF2
import os
import glob

def get_all_pdf_files(path):
  """获取指定目录的所有pdf文件名"""
  all_pdfs = glob.glob('{0}/*.pdf'.format(path))
  all_pdfs.sort(key=str.lower) # 排序
  return all_pdfs

def main():
  path = os.getcwd()
  all_pdfs = get_all_pdf_files(path)
  if not all_pdfs:
    raise SystemExit('没有可用的PDF类型文件')

  merger = PyPDF2.PdfFileMerger()

  first_obj = open(all_pdfs[0], 'rb') # 打开第一个PDF文件
  merger.append(first_obj) # 增加到合并的对象中

  file_objs = []
  for pdf in all_pdfs[1:]: # 读取所有的文件对象
    file_objs.append(open(pdf, 'rb'))

  for file_obj in file_objs:
    reader = PyPDF2.PdfFileReader(file_obj)
    merger.append(fileobj=file_obj, pages=(1, reader.getNumPages()))

  outputStream = open('merge-pdfs.pdf', 'wb')
  merger.write(outputStream)
  outputStream.close()
  for file_obj in file_objs: # 批量关闭文件句柄
    file_obj.close()

if __name__ == '__main__':
  main()